/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * 
 * @author yaoyuming
 * 
 * @date 2020-04-16 15:39
 */
package com.yao.common.config.converters;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 自定义HttpMessage消息转换
 * @author yaoyuming
 * @date 2020/04/16 15:39
 */
public class CustomMessageConverter {

    private final static  String ENCODING = "UTF-8";

    private final static String FORMATTER = "yyyy-MM-dd HH:mm:ssS";

    private CustomMessageConverter(){};

    /**
      * 功能描述 : 将@ResponseBody实体转Fastjson输出
      * @param
      * @author yaoyuming
      * @date 2020/6/16 0016
      * @return {@link FastJsonHttpMessageConverter}
      */
    public static FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {

        // 消息转换对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        // 配置信息（）
        FastJsonConfig config = new FastJsonConfig();

        config.setCharset(Charset.forName(ENCODING));
        config.setDateFormat(FORMATTER);

        config.setSerializerFeatures(
                SerializerFeature.SortField,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.DisableCircularReferenceDetect
        );

        fastConverter.setFastJsonConfig(config);


        // 针对java.sql.Time 序列化
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON);
        fastConverter.setSupportedMediaTypes(list);

        return fastConverter;
    }


    public static StringHttpMessageConverter stringHttpMessageConverter() {
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();

        //设置编码
        stringConverter.setDefaultCharset(Charset.forName(ENCODING));
        stringConverter.setWriteAcceptCharset(false);

        return stringConverter;
    }

}